<?php 
include_once 'functions.php';

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                                 RUN FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////
$getStudent = getStudent();
$getQuestion = getQuestion();
////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                              HANDLE ANSWER SUBMISSION
////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['submitter']))
{
$selectedAnswer = $_POST['questionOptions'];

   $checkAnswer = checkAnswer();
    $correctAnswer = $checkAnswer[0][0];
    if ($selectedAnswer == $correctAnswer) {
                        $score = ($_POST['score']+1);
        $_SESSION['correct'][]= $selectedAnswer;
                 $correct = $_SESSION['correct'];       
                
                $feedbackStatus = "<span class='green'> correct.</span>";
        } 
                
                else {
        $feedbackStatus = "<span class='red'> incorrect</span>, the correct answer was $correctAnswer.";
        $_SESSION['incorrect'][] = $selectedAnswer;
        $score = ($_POST['score']); 
                }
                
                
 $con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
                $query = "INSERT INTO studentresponse (attempt, studentID, selectedAnswer, questionID, testID, correctAnswer, startTime) VALUES ("
                . "'" . $_SESSION['attempt'] . "',"
                . "'" . $_SESSION['studentID'] . "',"
                . "'" . $selectedAnswer . "',"
                . "'" . $_POST['num']  . "',"
                . "'" . $testID  . "',"
                . "'" . $checkAnswer[0][0] . "', NOW()"
                . ")";  
$result = mysqli_query($con, $query);  
//$score = $_SESSION['score']; 
}

if (isset($_POST['solicitFeedback']))
{
    $num = $_POST['num'];
   $score = $_POST['score'];
        $_SESSION['score'] = $_POST['score']; 

} 


////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                      RECORD STUDENTINTEST
////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['submitter'])){
        if ($_POST['num'] < 2)
{
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
                $query = "INSERT INTO studentintest (attempt, studentID, testID, startTime) VALUES ("
        . "'" . $_SESSION['attempt']  . "',"
        . "'" . $_SESSION['studentID'] . "',"
        . "'" . $testID  . "',"
        . "NOW()" 
        . ")";  
$result = mysqli_query($con, $query);  
}
}

if (isset($_POST['results'])){
        $_SESSION['score'] = ($_POST['score']);                         
        $query2 = "UPDATE studentintest SET endTime="
        . "NOW()," 
        . " finalScore = " 
        . "'" . $_SESSION['score'] . "'"
        . " WHERE studentID = "
        . "'" . $_SESSION['studentID'] . "' AND testID =  "
        . "'" . $testID  . "'";

$result = mysqli_query($con, $query2); 
echo $query2;
if (isset($_POST['results']) && $_SESSION['attempt']==2 ){
header('LOCATION: result2.php');

}
else{
header('LOCATION: result.php');

}
}


?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="styles/styles.css" type="text/css" />
<title>mcq</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="scripts/scripts.js"></script>
</head>
<body>
<div id="container">
  <progress id="progress" value="<?php echo ($_POST['num']) ?>" max="11">
    <?php echo ($_POST['num'])?>
  </progress>
  <div id="mainContent">
    <?php echo "<h4>Good luck, {$_SESSION['firstName']}!</h4>"; ?>
    <div id="quiz">
      <?php if (!isset($_POST['submitter'])) { 
   if ($getQuestion) {
                $questionID = $getQuestion[0][0];
                $questionStem = $getQuestion[0][1];
                $option1 = $getQuestion[0][2];
                $option2 = $getQuestion[0][3];
                $option3 = $getQuestion[0][4];
                $option4 = $getQuestion[0][5];
                $resource = $getQuestion[0][7];

} 
          else {
          echo "<p class='crying'>Could not get item</p>";                      
          }  ?>
      <form id="question" method="post">
        <h2>Question
          <?php echo $num; ?>
        </h2>
        <?php if ($resource){ echo "<div id='resource'>" ?>
        <?php echo $resource ?>
        <?php echo '</div>'  ?>
        <?php } ?>
        <div class="q-stem">
          <p id="itemStem">
            <?php echo $questionStem ?>
          </p>
        </div>
        <div>
          <input name="questionOptions" id="radio1" type="radio" value="<?php echo $option1 ?>">
          <label id="label1" for="radio1">
            <?php echo $option1 ?>
          </label>
        </div>
        <div>
          <input name="questionOptions" id="radio2" type="radio" value="<?php echo $option2 ?>">
          <label id="option2" for="radio2">
            <?php echo $option2 ?>
          </label>
        </div>
        <div>
          <input name="questionOptions" id="radio3" type="radio" value="<?php echo $option3 ?>">
          <label id="option3" for="radio3">
            <?php echo $option3 ?>
          </label>
        </div>
        <div>
          <input name="questionOptions" id="radio4" type="radio" value="<?php echo $option4 ?>">
          <label id="option4" for="radio4">
            <?php echo $option4 ?>
          </label>
        </div>
        <input type="hidden" name="num" value="<?php echo $num ?>" />
        <input type="hidden" name="correct" value="<?php echo $_SESSION['correct']?>" />
                <input type="hidden" name="score" value="<?php echo $score ?>" />
        <input type="hidden" name="submitter" value="TRUE" />
        <input type="submit" id="submit" name="submit" value="Check Answer" />
      </form>
      <?php } elseif (isset($_POST['submitter'])) { $num = $_POST['num'];  $_SESSION['score'] = $score; if($_POST['num']>9){$last = true; $_SESSION['finished'] = 'yes';} ?>
      <form id="feedback" method="post">
        <h2>Question
          <?php echo ($_POST['num']) ?>
        </h2>
        <p>You answered:
          <?php echo $selectedAnswer ?>
        </p>
        <p>That was
          <?php echo $feedbackStatus?>
        </p>
        <?php if (!$last) {  ?>
        <input type="hidden" name="correct" value="<?php echo $correct ?>" />
        <input type="hidden" name="num" value="<?php echo ($num +1) ?>" />
        <input type="hidden" name="score" value="<?php echo $score ; ?>" />
        <input type="hidden" name="solicitFeedback" value="TRUE" />
        <input type="submit" id="submit" name="next" value="Next Question"  />
        <?php } else { ?>
        <input type="hidden" name="num" value="<?php echo ($num +1) ?>" />
        <input type="hidden" name="score" value="<?php echo $score ; ?>" />
        <input type="submit" id="results" name="results" value="Get Results" />
        <?php } ?>
      </form>
      <?php }?>
      <p class='smiling'>Incorrect Answers:
        <?php echo print_r(array_values($_SESSION['incorrect']))?>
      </p>
      <p class='smiling'>Correct Answers session:
        <?php echo print_r(array_values($_SESSION['correct']))?>
      </p>
      <p class='smiling'>Correct from posting:
        <?php echo $correct?>
      </p>
      <p class='smiling'>Selected Answers:
        <?php echo $selectedAnswer ?>
      </p>
      <p class='smiling'>Score:
        <?php echo $score ?>
      </p>
      <p class='smiling'>Num:
        <?php echo $num ?>
      </p>
      <p class='smiling'>Last:
        <?php echo $last ?>
        |
        <?php echo ($_SESSION['finished'])?>
      </p>
      <p class='smiling'>Session score:
        <?php echo ($_SESSION['score'])?>
      </p>
      <p class='smiling'>Student ID:
        <?php echo ($_SESSION['studentID'])?>
      </p>
      <p class='smiling'>Attempt:
        <?php echo ($_SESSION['attempt'])?>
      </p>
    </div>
    <!--mainContent--> 
  </div>
  <!--quiz--> 
</div>
<!--container-->
</body>
</html>
